import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/DynamicService.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/views/Dynamic/DynamicItem.dart';
import 'package:thinking/views/Dynamic/RecentVisit.dart';
import 'package:thinking/widget/_.dart';

class DynamicList extends StatefulWidget {
  final int index;

  DynamicList({this.index = 0});

  @override
  _DynamicList createState() => _DynamicList(index: index);
}

class _DynamicList extends State<DynamicList>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;
  late int index;
  late DynamicResult _dynamicResult;
  late DynamicPageEntity _dynamicPageEntity;
  bool isGetDynamicData = false;
  late EasyRefreshController _easyRefreshController;
  _DynamicList({this.index = 0});

  @override
  void initState() {
    super.initState();
    _dynamicResult = DynamicResult();
    _dynamicPageEntity = DynamicPageEntity();
    _easyRefreshController = EasyRefreshController();
    getDynamicData();

  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
    super.dispose();
  }

  Future<void> getDynamicData(
      {bool isOnRefresh = false, bool isOnLoad = false}) async {
    var res = await DynamicService.getDynamicData(params: {
      "tag_Page": _dynamicPageEntity.tagPage,
      "tag_Count": _dynamicPageEntity.tagCount,
      "dynamic_Count": _dynamicPageEntity.dynamicCount,
      "dynamic_Page": _dynamicPageEntity.dynamicPage,
      "dynamic_status": index,
      "user_Id": SPreferences().getInt('user_Id')
    });
    if ((res!.topicList != null ? res.topicList!.length : 0) <
        _dynamicPageEntity.tagCount) {
      _dynamicPageEntity.setCanTag = false;
    }
    if ((res.dynamicList != null ? res.dynamicList!.length : 0) <
        _dynamicPageEntity.dynamicCount) {
      _dynamicPageEntity.setCanDynamic = false;
    }
    setState(() {
      if (isOnLoad) {
        if (!_dynamicPageEntity.canDynamic) {
          BotToast.showText(text: '没有更多动态了哦~');
        } else {
          BotToast.showText(text: '更新了${res.dynamicList!.length}条动态~');
        }
        _dynamicResult.setDynamicList = [
          ...?_dynamicResult.dynamicList,
          ...?res.dynamicList
        ];
      } else {
        _dynamicResult.setDynamicList = res.dynamicList!;
        BotToast.showText(text: '更新了${res.dynamicList!.length}条动态~');
        if (_dynamicPageEntity.canTag)
          _dynamicResult.setTopicList = res.topicList!;
      }
      if (res.recentVisitList != null)
        _dynamicResult.setRecentVisitList = res.recentVisitList!;
      isGetDynamicData = true;
    });
  }

  Widget easyRefresh(Widget child) {
    return EasyRefresh(
      child: child,
      header: BezierCircleHeader(backgroundColor: ColorConfig.ThemeColor),
      footer: BezierBounceFooter(backgroundColor: ColorConfig.ThemeColor),
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _easyRefreshController,
      bottomBouncing: true,
      onRefresh: () async {
        if (!this.isGetDynamicData) return;
        _dynamicPageEntity.setDynamicPage = 0;
        _dynamicResult.setDynamicList = [];
        _dynamicPageEntity.setCanDynamic = true;
        if (_dynamicPageEntity.canTag)
          _dynamicPageEntity.setTagPage = _dynamicPageEntity.tagPage + 1;
        await getDynamicData(isOnRefresh: true);
        _easyRefreshController.finishRefresh(success: true);
      },
      onLoad: () async {
        if (!this.isGetDynamicData) return;
        if (_dynamicPageEntity.canDynamic)
          _dynamicPageEntity.setDynamicPage =
              _dynamicPageEntity.dynamicPage + 1;
        await getDynamicData(isOnLoad: true);
        _easyRefreshController.finishLoad(success: true);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return this.isGetDynamicData
        ? easyRefresh(
            Empty(
              isShow: _dynamicResult.dynamicList!.length == 0,
              child: ListView(
                shrinkWrap: true,
                physics: BouncingScrollPhysics(),
                children: [

                       Visibility(child: Column(
                    children: [
                      Visibility(
                          visible:index == 0 ,
                          child: SizedBox(
                            height: 15,
                          )),

                      Visibility(
                          visible: this._dynamicResult.recentVisitList!.length != 0,
                          child: RecentVisit(
                            recentVisitList:
                            _dynamicResult.recentVisitList!,
                          ))
                      ,

                      Visibility(child: Line(),visible: this._dynamicResult.recentVisitList!.length != 0,)
                      ,

                      this._dynamicResult.topicList!=null?Topic(
                        topicList: _dynamicResult.topicList!,
                        title: '热门话题',
                        showMore: true,
                        clickTopicItem:
                            (int id, String e, int isSub) {
                          Modular.to.pushNamed(
                              '/topicIndex/$id/$e/$isSub');
                        },
                      ):Container(),
                    ],
                  ),visible: index == 0,),
                  Column(
                    children: _dynamicResult.dynamicList!.asMap().keys
                        .map((e) => Column(
                              children: [
                                Visibility(child: Line(),visible: !(index==1&&e==0)),
                                DynamicItem(
                                  dynamicData:_dynamicResult.dynamicList![e],
                                  key: ValueKey(_dynamicResult.dynamicList![e].dynamicId),
                                )
                              ],
                            ))
                        .toList(),
                  )
                ],
              ),
            ),
          )
        : Loading();
  }
}
